#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sstream>

// Measure time consumed by some part of a program
// to compile: g++ -lboost_date_time-mt datetime.cpp

void f()
{
  // do something to kill some time
  std::size_t counter = 0;

  while (counter < 1900000)
    {
      std::size_t a = 200 * 400 * 300 - 123*456 + 789*123;
      ++counter;
    }
}

std::string time_in_secs(const boost::posix_time::time_duration& td)
{
  long in_secs = td.total_milliseconds();

  std::stringstream out;

  long secs = in_secs / 1000;
  long rest = in_secs % 1000;

  out << secs << ".";

  if (rest < 10) 
    {
      out << "00";
    }
  else if (rest < 100)
    {
      out << "0";
    }

  out << rest;
    
  return out.str();
}

int main()
{
  boost::posix_time::time_duration t0(0, 0, 0, 0);
  std::cout << boost::posix_time::to_simple_string(t0) << std::endl;

  boost::posix_time::ptime t1 = boost::posix_time::microsec_clock::local_time();
  f();
  boost::posix_time::ptime t2 = boost::posix_time::microsec_clock::local_time();

  boost::shared_ptr<boost::posix_time::time_duration> td(new boost::posix_time::time_duration);

  boost::posix_time::time_duration td1(0, 1, 0, 0);

  *td = td1 + (t2 - t1);

  std::cout << boost::posix_time::to_simple_string(t2) << " - "
	    << boost::posix_time::to_simple_string(t1) << " = "
	    << boost::posix_time::to_simple_string(*td) << " => " 
	    << time_in_secs(*td) << std::endl;

}
